import re

import jieba
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from wordcloud import WordCloud

with open('./file/content.txt', 'r', encoding='utf-8') as f:
    txt = f.read()
    # 如果是文章的话，需要用到jieba分词，分完之后也可以自己处理下再生成词云
    new_txt = re.sub('[A-Za-z0-9\!\%\[\]\,\。]', '', txt)

    words = jieba.lcut(new_txt)

    # 想要搞得形状
    img = Image.open(r'./img/wc.jpg')
    img_array = np.array(img)

# 相关配置，里面这个collocations配置可以避免重复
wordcloud = WordCloud(
    # background_color="red",
    width=1080,
    height=960,
    font_path="./WenYue-XinQingNianTi-W8-J-2.otf",
    max_words=150,
    scale=10,  # 清晰度
    max_font_size=100,
    mask=img_array,
    collocations=False).generate(new_txt)

plt.imshow(wordcloud)
plt.axis('off')
plt.show()
wordcloud.to_file('./img/wc.png')
